<?php
class Zdvluyke1sController extends AppController {

	var $name = 'Zdvluyke1s';
	var $helpers = array('Html', 'Form','Xephang','Js');
    var $uses=array('Zdvluyke1','Thuocdv','Thoigian','Donvi');
  
	function index($thoigian=null,$id_donvi=null) {
		$nam = $this->Thoigian->find('list',array(
	    										'fields'=>array('Thoigian.ngaybd'),
	    										'group'=>array('YEAR(Thoigian.ngaybd)'),
	    										'order'=>array('ngaybd'=>'DESC')));
    		foreach ($nam as $key => $value) 
    		{
    			$nams[date('Y',strtotime($value))]=date('Y',strtotime($value)); 
    		}
		$namht = date('Y');          
       $dv=$this->Donvi->find('list');
       
       $tg=$this->Thoigian->find('list',array('conditions'=>array('thoigian_id'=>0,'YEAR(Thoigian.ngaybd)'=>$namht),'order'=>array('thoigian'=>'DESC')));
       $this->set(compact('dv','tg','nams'));
        
	}

    function indexexcel($thoigian,$id_donvi=null) {
        
        $tendv=$this->Donvi->find('first',array(
                    'conditions'=>array('id'=>$id_donvi),'recursive'=>-1, 'fields'=>array('tendv')));
        $tenthang=$this->Thoigian->find('first',array(
                    'conditions'=>array('id'=>$thoigian),
                    'fields'=>array('ngaybd','ngaykt'),'recursive'));            
        $dvs=$this->Thuocdv->find('list',array(
                    'fields'=>array('donvi','donvi'),
                    'conditions'=>array('donvi_id'=>$id_donvi)));
                    
		$this->Zdvluyke1->recursive = -1;	
        $dulieu= $this->Zdvluyke1->find('all',array(
                    'conditions'=>array('thoigian_id'=>$thoigian,'donvi_id'=>$dvs),
                    'fields'=>array('chitieu_n','kqban_n','tyle'),
                    'order'=>array('tenhh'=>'asc','tendv'=>'asc')));
        
        $donvis=$this->Zdvluyke1->find('list',array(
                    'conditions'=>array('thoigian_id'=>$thoigian,'donvi_id'=>$dvs),
                    'fields'=>array('tendv','tendv'),
                    'order'=>array('tendv')));
        $hanghoas=$this->Zdvluyke1->find('list',array(
                    'conditions'=>array('thoigian_id'=>$thoigian,'donvi_id'=>$dvs),
                    'fields'=>array('tenhh','diem'),
                    'order'=>array('tenhh')));
      //  print_r($dulieu);
       // print_r($donvis);
       // print_r($hanghoas);
       $sogroup=count($hanghoas);
       $sophantu_trong_group=count($donvis);
       $group=array();
      
       for($i=1;$i<=$sogroup;$i++){
               for($j=1;$j<=$sophantu_trong_group;$j++){ 
                  $group[$i-1][$j-1]=$dulieu[($sophantu_trong_group)*($i-1)+$j-1]['Zdvluyke1']['tyle'];
                  
                  //print_r('so'.($sophantu_trong_group)*($i-1)+$j-1);
               }
       }
     //  print_r($group);
            
       $this->layout='';
       $this->set(compact('dulieu','donvis','hanghoas','group','dl','tendv','tenthang'));
        
	}

	function updatekq()
	{
		$this->layout='ajax';
		if (isset($this->data['Zdvluyke1']['thoigian_id']))
			$thoigian=$this->data['Zdvluyke1']['thoigian_id'];
		else
				$thoigian=$this->data['thoigian_id'];
				
        $id_donvi=$this->data['Zdvluyke1']['donvi_id'];
            
            $dvs=$this->Thuocdv->find('list',array(
                    'fields'=>array('donvi','donvi'),
                    'conditions'=>array('donvi_id'=>$id_donvi)));
                    
            $this->Zdvluyke1->recursive = -1;
            	
            $dulieu= $this->Zdvluyke1->find('all',array(
                    'conditions'=>array('thoigian_id'=>$thoigian,'donvi_id'=>$dvs),
                    'fields'=>array('chitieu_n','kqban_n','tyle'),
                    'order'=>array('tenhh'=>'asc','tendv'=>'asc')));
        
            $donvis=$this->Zdvluyke1->find('list',array(
                    'conditions'=>array('thoigian_id'=>$thoigian,'donvi_id'=>$dvs),
                    'fields'=>array('tendv','tendv'),
                    'order'=>array('tendv')));
            $hanghoas=$this->Zdvluyke1->find('list',array(
                    'conditions'=>array('thoigian_id'=>$thoigian,'donvi_id'=>$dvs),
                    'fields'=>array('tenhh','diem'),
                    'order'=>array('tenhh')));
      //  print_r($dulieu);
       // print_r($donvis);
       // print_r($hanghoas);
            $sogroup=count($hanghoas);
            $sophantu_trong_group=count($donvis);
            $group=array();
      
            for($i=1;$i<=$sogroup;$i++){
               for($j=1;$j<=$sophantu_trong_group;$j++){ 
                  $group[$i-1][$j-1]=$dulieu[($sophantu_trong_group)*($i-1)+$j-1]['Zdvluyke1']['tyle'];
                  
                  //print_r('so'.($sophantu_trong_group)*($i-1)+$j-1);
               }
            }
     //  print_r($group);
     $this->set(compact('dulieu','donvis','hanghoas','group','dl','thoigian','id_donvi','dv','tg','kt'));
	}

	function updatethoigian()
	{
		$this->layout='ajax';
		$namht = $this->data['Zdvluyke1']['nam'];
		$tg=$this->Thoigian->find('list',array('conditions'=>array('thoigian_id'=>0,'YEAR(Thoigian.ngaybd)'=>$namht),'order'=>array('thoigian'=>'DESC')));
    	$this->set(compact('tg'));
	}
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Zdvluyke1.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('zdvluyke1', $this->Zdvluyke1->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Zdvluyke1->create();
			if ($this->Zdvluyke1->save($this->data)) {
				$this->Session->setFlash(__('The Zdvluyke1 has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Zdvluyke1 could not be saved. Please, try again.', true));
			}
		}
		$donvis = $this->Zdvluyke1->Donvi->find('list');
		$thoigians = $this->Zdvluyke1->Thoigian->find('list');
		$this->set(compact('donvis', 'thoigians'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Zdvluyke1', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Zdvluyke1->save($this->data)) {
				$this->Session->setFlash(__('The Zdvluyke1 has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Zdvluyke1 could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Zdvluyke1->read(null, $id);
		}
		$donvis = $this->Zdvluyke1->Donvi->find('list');
		$thoigians = $this->Zdvluyke1->Thoigian->find('list');
		$this->set(compact('donvis','thoigians'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Zdvluyke1', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Zdvluyke1->del($id)) {
			$this->Session->setFlash(__('Zdvluyke1 deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>